package com.jeff.android.lab.module.anim.property;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeAnimator;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: duff
 * @date: 2019/1/14
 * @since: 1.0.0
 */
public class PropertyAnimFragment extends BaseFragment {

    @BindView(R.id.iv_target)
    ImageView mIvTarget;

    @Override
    protected int getRootLayout() {
        return R.layout.anim_fragment_property;
    }

    @OnClick(R.id.btn_alpha)
    public void alpha() {
        execAnim(mIvTarget, "alpha");
    }

    @OnClick(R.id.btn_rotation)
    public void rotation() {
        execAnim(mIvTarget, "rotation");
    }

    @OnClick(R.id.btn_scale)
    public void scale() {
        execAnim(mIvTarget, "scale");
    }

    @OnClick(R.id.btn_translation)
    public void translation() {
        execAnim(mIvTarget, "translation");
    }

    @OnClick(R.id.btn_play_together)
    public void playTogether() {
        execAnimTogether(mIvTarget, false);
    }

    @OnClick(R.id.btn_play_sequentially)
    public void playSequentially() {
        execAnimTogether(mIvTarget, true);
    }

    @OnClick(R.id.btn_xml_anim)
    public void playXmlAnim() {
        Animator animator = AnimatorInflater.loadAnimator(getContext(),
                R.animator.anim_property_scale);

        animator.setTarget(mIvTarget);
        animator.start();
    }

    private void execAnim(View target, String property) {
        ObjectAnimator animator = null;

        AnimatorSet animatorSet = new AnimatorSet();

        switch (property) {
            case "alpha":
                animator = ObjectAnimator.
                        ofFloat(target, "alpha", 1.0f, .0f, 1.0f);
                break;

            case "rotation":
                animator = ObjectAnimator.
                        ofFloat(target, "rotation", .0f, 360.0f);
                break;

            case "scale":
                ObjectAnimator scaleX = ObjectAnimator
                        .ofFloat(target, "scaleX", 1.0f, 1.5f, 1.0f);
                ObjectAnimator scaleY = ObjectAnimator
                        .ofFloat(target, "scaleY", 1.0f, 1.5f, 1.0f);
                animatorSet.playTogether(scaleX, scaleY);
                break;

            case "translation":
                animator = ObjectAnimator.
                        ofFloat(target, "translationX", .0f, 500f, .0f);
                break;
        }

        if (animator != null) {
            animator.setDuration(3000l);
            animator.setInterpolator(new AccelerateDecelerateInterpolator());
            animator.start();
        }

        if (animatorSet != null) {
            animatorSet.setDuration(3000l);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.start();
        }

//        mIvTarget.settr
//        TimeAnimator
    }

    private void execAnimTogether(View target, boolean sequentially) {
        ObjectAnimator alpha = ObjectAnimator.
                ofFloat(target, "alpha", 1.0f, .0f, 1.0f);

        ObjectAnimator rotation = ObjectAnimator.
                ofFloat(target, "rotation", .0f, 360.0f);

        ObjectAnimator scaleX = ObjectAnimator
                .ofFloat(target, "scaleX", 1.0f, 1.5f, 1.0f);

        ObjectAnimator scaleY = ObjectAnimator
                .ofFloat(target, "scaleY", 1.0f, 1.5f, 1.0f);

        ObjectAnimator translation = ObjectAnimator.
                ofFloat(target, "translationX", .0f, 500f, .0f);

        AnimatorSet animatorSet = new AnimatorSet();
        if (sequentially) {
            animatorSet.playSequentially(alpha, rotation, scaleX, scaleY, translation);
        } else {
            animatorSet.playTogether(alpha, rotation, scaleX, scaleY, translation);
        }
        animatorSet.setDuration(3000l);
        animatorSet.setInterpolator(new LinearInterpolator());
        animatorSet.start();
    }


    @OnClick(R.id.btn_value_animator)
    public void start() {
        ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f, 1.0f);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float value = (Float) animation.getAnimatedValue();

                Log.d("jeff", "value:" + value);

                mIvTarget.setAlpha(value);
            }
        });

        animator.setDuration(3000l);
        animator.start();

//        ValueAnimator.ofArgb(0xff0000, 0x00ff00);

//        ObjectAnimator.
    }


}
